09. Lesson Review
In this lesson we learned…
How can we rotate a gameobject through a script?
There are a couple of different ways we could rotate objects in scripts. In our script, we’ll need to create a public reference to the object we plan to rotate. Then we’ll need to assign it in the inspector.
- Use the Rotate method.
If we wanted to use the Rotate method to rotate an object really slowly around the Y axis, our code would look something like this.:
public GameObject objectToRotate;
void Update() {
objectToRotate.transform.Rotate(0f, Time.deltaTime, 0f);
}
- Use Slerp.
Using Slerp, we must define the start and ending rotations (these are stored as Quaternions).
We also need to define a time value for the Slerp method. This time value must be a number between 0 and 1. If it’s 0, it’ll stay at the start rotation, and if it’s 1 it’ll stay at the end. Can you guess what it’ll be if we enter 0.5? If you guessed in the middle of the two points, you’re right. To calculate this time value, we’ll use Time.time and divide it by a value. In the example here, I used 50. This will make the rotation much slower. Using a smaller number, will make the rotation faster.
public GameObject objectToRotate;
public float timeScale = 50f;
void Update() {
Quaternion startRotation = Quaternion.Euler(50f, 30f, 0f);
Quaternion endRotation = startRotation * Quaternion.Euler(0f, 180f, 0f);
objectToRotate.transform.rotation = Quaternion.Slerp(startRotation, endRotation, Time.time / timeScale);
}
Tip: To add two Quaternion’s together, you need to multiply them.
What is interpolation, “lerp”, and “slerp”?
Interpolation is the process of creating data points between two given points. For example, if we have two points A and B that are sitting on a table 1 foot apart. The process of interpolation would be the process of creating the points that lead from point A to point B. For example, these might be points 1 inch apart along the distance between the two points.
Lerp is “linear interpolation” and Slerp is “spherical linear interpolation”. Both use a time variable to determine how much to interpolate. Lerp uses two points, and interpolates in a straight line from point A to point B. Slerp, on the other hand, also uses two points, but interpolates in an arch from point A to point B. Slerp could be used, for example, to animate a rising and setting sun.
What are variables?
A variables are names that we assign to a data type so that it’s stored in the computer’s memory for us to use later. In C# we have to not only give a variable a name, but also decide what that type of data that variable will hold. For example:
public bool partyStarted = false; // Booleans can also be true.
public int numberOfBalloons = 5; // This could be any whole number.
public string message = “Happy Birthday!”; // This could be any text surrounded with quotation marks.
public Vector3 messagePosition = new Vector3(0, 1, 2); // Any position in 3D space.
public GameObject present; // Any object or prefab that we’ll define in the Unity editor.
How can we control our Animator in a script?
There are a lot of ways we can interact with our Animator.
Stop or Start Animator Component
If we wanted to stop our animator, we could do that with the enabled property.
public Animator animator;
public void StopOurAnimator() {
animator.enabled = false;
}
public void StartOurAnimator() {
animator.enabled = true;
}
Play An Animation
If we wanted to play a specific animation, we could do that with the Play method. We’d need to provide the name of the animation state to play.
public Animator animator;
public void StartOurAnimator() {
animator.Play(“animationStateName”);
}
Set Animation Parameters
A lot of times we’ll use parameters in our animator. We can edit those through scripts as well. For example, let’s say we have a boolean parameter in our Animator called “isLocked”. We have an animation set up to play when the isLocked is false. In our code, we’d use the Animator’s SetBool method.
public Animator animator;
public void Unlock() {
animator.SetBool(“isLocked”, false);
}
Tip: The animator has other methods for different types of animator parameters. These include SetTrigger, SetFloat, and SetInt. The only one that is used differently is the SetTrigger. Instead of providing two values like the example above (which has the parameter name and the value we want to set it to), the SetTrigger method only needs the parameter name.
To learn more about what the Animator can do, you can take a look at the official documentation here.